/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			varstore.h
  \date			Dec 2011
  \author		TNick

  \brief		Contains the definition for VarStore class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef __VARSTORE_INC__
#define __VARSTORE_INC__
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QString>
#include	<QVariant>
#include	<QRgb>
#include	<QColor>

#include	<storetype.h>
#include	<userary.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

namespace	PgScr	{
class	DefVar;

/// class representing a storage space capable of holding various elements
/**
*
*/
class VarStore				{

	//
	//
	//
	//
	/*  DEFINITIONS    ----------------------------------------------------- */



	/*  DEFINITIONS    ===================================================== */
	//
	//
	//
	//
	/*  DATA    ------------------------------------------------------------ */

public:

	/**
	*	@brief	the type of the underlying storage for this variable
	*/
	StoreType		ty;


	union		{

		/**
		*	@brief	integer
		*/
		qint64			l;


		/**
		*	@brief	boolean value
		*/
		bool			b;


		/**
		*	@brief	a character
		*/
		int				ch;


		/**
		*	@brief	color in rgb format
		*/
		QRgb			rgb;


		/**
		*	@brief	a real number in floating point format
		*/
		qreal			d;


		/**
		*	@brief	an unicode based string
		*/
		UserAry			s;


		/**
		*	@brief	an aray;
		*/
		UserAry			ary;


		/**
		*	@brief	a DefVar inheriting class;
		*/
		DefVar *		dfv;


	};

	/*  DATA    ============================================================ */
	//
	//
	//
	//
	/*  FUNCTIONS    ------------------------------------------------------- */




public:

	//@{
	//! constructor

	VarStore		( void )
	{}

	inline VarStore	( const qint64 new_val )
	{ l = new_val; ty = VTY_INTEGER; }

	inline VarStore	( const int new_val )
	{ l = new_val; ty = VTY_INTEGER; }

	inline VarStore	( const bool new_val )
	{ b = new_val; ty = VTY_BOOL; }

	inline VarStore	( const char new_val )
	{ ch = new_val; ty = VTY_CHARACTER; }

	inline VarStore	( const QRgb new_val )
	{ rgb = new_val; ty = VTY_COLOR; }

	inline VarStore	( const QColor new_val )
	{ set( new_val.rgb() ); }

	inline VarStore	( const QString new_val )
	{ ty = VTY_STRING; s.fromString( new_val ); }

	inline VarStore	(const char * new_val)
	{ ty = VTY_STRING; s.fromString( QString(new_val) ); }

	inline VarStore	( const qreal new_val )
	{ d = new_val; ty = VTY_DOUBLE; }

	//@}


	/// destructor;
	~VarStore		( void )
	{}


	/**
	*	@brief	get the basic type of this storage
	*/
	inline StoreType	basicType		( void )
	{ return (StoreType)(ty & VTY_BASE_MASK); }


	/**
	*	@brief	get the composite type of this storage
	*/
	inline StoreType	compositeType	( void )
	{ return (StoreType) ( ( ty & VTY_COMP_MASK ) >> 8 ); }


	/**
	*	@brief	get the level count from type
	*/
	inline int			level	( void )
	{ return ( ( ty & VTY_LEVEL_MASK ) >> 16 ); }


	/**
	*	@brief	set the ground state ( integer 0 )
	*/
	inline void			setGroundState	( void )
	{ ty = VTY_INTEGER; l = 0; }


	/**
	*	@brief	make a composite type from two basic types
	*
	*	The base is expected to be one of the types that uses multiple values
	*	@param	base	the one that will be retrieved by basicType()
	*	@param	ext		the one that will be retrieved by compositeType()
	*	@param	level	number of nested levels
	*/
	static inline StoreType	makeCompositeType	(
			StoreType		base,
			StoreType		ext,
			int				level = 0
			)
	{
		Q_ASSERT( base == VTY_FLAG_ARRAY );
		/* add here other types that are legal in this respect*/

		Q_ASSERT( ext < 0xFF );
		Q_ASSERT( base < 0xFF );
		Q_ASSERT( level < 0xFFFF );

		return ( StoreType ) ( ( level << 16 ) | ( ext << 8 ) | base );
	}




	//@{
	//! set the instance to a definitive value; sets both value and type

	inline void			set	( const qint64 new_val )
	{ l = new_val; ty = VTY_INTEGER; }

	inline void			set	( const int new_val )
	{ l = new_val; ty = VTY_INTEGER; }

	inline void			set	( const bool new_val )
	{ b = new_val; ty = VTY_BOOL; }

	inline void			set	( const char new_val )
	{ ch = new_val; ty = VTY_CHARACTER; }

	inline void			set	( const QRgb new_val )
	{ rgb = new_val; ty = VTY_COLOR; }

	inline void			set	( const QColor new_val )
	{ set( new_val.rgb() ); }

	inline void			set	( const QString new_val )
	{ ty = VTY_STRING; s.fromString( new_val ); }

	inline void			set	(const char * new_val)
	{ set( QString(new_val) ); }

	inline void			set	( const qreal new_val )
	{ d = new_val; ty = VTY_DOUBLE; }

	inline void			setChar( const int new_val )
	{ ch = new_val; ty = VTY_CHARACTER; }

	inline void			setColor( const uint new_val )
	{ rgb = new_val; ty = VTY_COLOR; }

	//@}


	/*  FUNCTIONS    ======================================================= */
	//
	//
	//
	//

};	/*	class VarStore	*/

/*  CLASS    =============================================================== */
//
//
//
//

}	//	namespace	PgScr

#endif // __VARSTORE_INC__
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
